﻿using System;
using System.Windows;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

namespace JiveMessenger.CustomControls
{
    public class ProgressOverlay : ContentControl
    {
        private Storyboard _fadeIn;
        private Storyboard _fadeOut;
        private Grid _layoutGrid;

        private const string FadeInName = "FadeInStoryboard";
        private const string FadeOutName = "FadeOutStoryboard";
        private const string LayoutGridName = "LayoutGrid";

        public ProgressOverlay()
        {
            DefaultStyleKey = typeof(ProgressOverlay);
        }

        public object ProgressControl
        {
            get { return (object)GetValue(ProgressControlProperty); }
            set { SetValue(ProgressControlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ProgressControl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressControlProperty =
            DependencyProperty.Register("ProgressControl", typeof(object), typeof(ProgressOverlay), new PropertyMetadata(null));




        public double ProgressValue
        {
            get { return (double)GetValue(ProgressValueProperty); }
            set { SetValue(ProgressValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ProgressValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ProgressValueProperty =
            DependencyProperty.Register("ProgressValue", typeof(double), typeof(ProgressOverlay), new PropertyMetadata(0));



        public bool IsProgressOverlayVisible
        {
            get { return (bool)GetValue(IsProgressOverlayVisibleProperty); }
            set { SetValue(IsProgressOverlayVisibleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsProgressOverlayVisible.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsProgressOverlayVisibleProperty =
            DependencyProperty.Register("IsProgressOverlayVisible", typeof(bool), typeof(ProgressOverlay), new PropertyMetadata(false,IsProgressOverlayVisiblePropertyChanged));

        private static void IsProgressOverlayVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ProgressOverlay overlay = (ProgressOverlay)d;
            if (e.NewValue != null && ((bool)e.NewValue) == true)
            {
                overlay.Show();
            }
            else if (e.NewValue != null && ((bool)e.NewValue) == false)
            {
                overlay.Hide();
            }
        }


        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _fadeIn = GetTemplateChild(FadeInName) as Storyboard;
            _fadeOut = GetTemplateChild(FadeOutName) as Storyboard;
            _layoutGrid = GetTemplateChild(LayoutGridName) as Grid;

            if (_fadeOut != null)
                _fadeOut.Completed += FadeOutCompleted;
        }


        private void FadeOutCompleted(object sender, object e)
        {
            _layoutGrid.Opacity = 1;
            Visibility = Visibility.Collapsed;
        }


        public void Show()
        {
            if (_fadeIn == null)
                ApplyTemplate();

            Visibility = Visibility.Visible;

            if (_fadeOut != null)
                _fadeOut.Stop();

            if (_fadeIn != null)
                _fadeIn.Begin();
        }

        public void Hide()
        {
            if (_fadeOut == null)
                ApplyTemplate();

            if (_fadeIn != null)
                _fadeIn.Stop();

            if (_fadeOut != null)
                _fadeOut.Begin();
        }
    }
}
